//import Log from "../../utils/log";
import FileTypes from "../../context/file_types";

function GPSHandler() {
    this._viewer = window.cesiumViewer;
    this._pointEntitys = null;
    this._currentPointEntity = null;
    this._showGPSTrack = false;
    this._recordGPSTrack = false;
    this._intervalgps111 = null;
    this._dataSource = null;
}
GPSHandler.__gpsHandler = null;

//var _saveGPS = [];
// GPSHandler.getOrCreateInstance = function() {
//     if (GPSHandler.__gpsHandler == null) {
//         GPSHandler.__gpsHandler = new GPSHandler();
//     }
//     return GPSHandler.__gpsHandler;
// };

GPSHandler.getInstance = function() {
    if (GPSHandler.__gpsHandler == null) {
        GPSHandler.__gpsHandler = new GPSHandler();
    }
    return GPSHandler.__gpsHandler;
};

GPSHandler.getGPSData = function() {};
GPSHandler.prototype.importGps = function() {
    let that = this;
    JsContext.slotSelectGpsFile = function(path) {
        var str = JavaContext.ReadTxtFile(path);
        if (str == null || str.length < 3) {
            GPSHandler.getGPSData({ message: "获取GPS内容失败！" });
        }
        let res = that.parseGPSData(str);
        let rstr = "导入成功";
        if (!res) {
            rstr = "导入失败";
        }
        GPSHandler.getGPSData(rstr);
    };
    JavaContext.loadFileBrowser(FileTypes.GPS.isFile, FileTypes.GPS.rootPath, FileTypes.GPS.pathAll, "JsContext.slotSelectGpsFile");
};

GPSHandler.outputGps = function() {
    /*let str = JavaContext.outputGPSFile();*/
    //return JavaContext.saveGPSData(JSON.stringify(_saveGPS));
};

GPSHandler.prototype.clearData = function() {
    //_saveGPS = [];
    if (this._dataSource) {
        this._dataSource.entities.removeAll();
        this._viewer.dataSources.remove(this._dataSource);
        this._dataSource = null;
    }
};

// GPSHandler.prototype.getPositionInterval = function(a) {
//     let that = this;
//     this._pointEntitys = [];
//     clearInterval(this._intervalgps);
//     this._intervalgps = window.setInterval(function() {
//         let str = JavaContext.getCurrentGPS();
//         if (str.length < 1) return;
//         let pos = JSON.parse(str);
//         let pt = new Cesium.Entity({
//             name: "trajectory",
//             position: Cesium.Cartesian3.fromDegrees(pos.lon, pos.lat),
//             point: {
//                 color: Cesium.Color.ORANGE, //点位颜色
//                 outlineColor: Cesium.Color.WHITE,
//                 outlineWidth: 2,
//                 pixelSize: 12 //像素点大小
//             }
//         });
//
//         that._viewer.entities.add(pt);
//         if (that._currentPointEntity == null) {
//             that._viewer.camera.flyTo({
//                 //相机飞往该点
//                 destination: Cesium.Cartesian3.fromDegrees(pos.lon, pos.lat, 50000), //摄像机的最终位置
//                 duration: 3,
//                 orientation: new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(0), Cesium.Math.toRadians(-89), Cesium.Math.toRadians(0))
//             });
//         } else {
//             if (!that._showGPSTrack) {
//                 that._viewer.entities.remove(that._currentPointEntity);
//             } else {
//                 that._pointEntitys.push(that._currentPointEntity);
//             }
//         }
//         that._currentPointEntity = pt;
//     }, a * 1000);
// };

GPSHandler.prototype.setShowGPSTrack = function(b) {
    this._showGPSTrack = b;
    if (!this._dataSource) {
        return;
    }
    if (b && !this._viewer.dataSources.contains(this._dataSource)) {
        this._viewer.dataSources.add(this._dataSource);
    }
    if (!b) {
        this._viewer.dataSources.remove(this._dataSource);
    }
};

GPSHandler.prototype.getServiceStatus = function() {
    return JavaContext.getGPSServiceStatus();
};

GPSHandler.prototype.getShowGPSTrack = function() {
    return this._showGPSTrack;
};

GPSHandler.prototype.getRecordGPSEnabled = function() {
    return this._intervalgps111 != null;
};

GPSHandler.prototype.setServiceEnabled = function(sec) {
    return JavaContext.setGPSServiceEnable(sec);
};

GPSHandler.prototype.setRecordGPSEnabled = function(b, a) {
    if (b && this._intervalgps111 == null) {
        let that = this;
        this._intervalgps111 = window.setInterval(function() {
            let str = JavaContext.getCurrentGPS();
            let pos = JSON.parse(str);
            // let obj = {
            //     lat: pos.lat,
            //     lon: pos.lon,
            //     time: JSON.stringify(new Date().getTime())
            // };
            // _saveGPS.push(obj);
            let pt = new Cesium.Entity({
                name: "GPS",
                position: Cesium.Cartesian3.fromDegrees(pos.lon, pos.lat),
                point: {
                    color: Cesium.Color.ORANGE, //点位颜色
                    outlineColor: Cesium.Color.WHITE,
                    outlineWidth: 1,
                    pixelSize: 5 //像素点大小
                }
            });
            if (that._dataSource == null) {
                that._dataSource = new Cesium.CustomDataSource("GNSS");
                that.setShowGPSTrack(true);
            }
            that._dataSource.entities.add(pt);
        }, a * 1000);
    }
    if (!b) {
        if (this._intervalgps111) {
            clearInterval(this._intervalgps111);
        }
        this._intervalgps111 = null;
        this.clearData();
    }
};

GPSHandler.prototype.handle = function() {
    let str = JavaContext.getChangedGpsData();
    this.parseGPSData(str);
};

GPSHandler.prototype.parseGPSData = function(jsonstr) {
    let arr = JSON.parse(jsonstr);
    if (arr.length < 1) return false;
    let that = this;
    arr.forEach(function(item) {
        let pt = new Cesium.Entity({
            name: "trajectory",
            position: Cesium.Cartesian3.fromDegrees(item.lon, item.lat),
            point: {
                color: Cesium.Color.ORANGE, //点位颜色
                outlineColor: Cesium.Color.WHITE,
                outlineWidth: 1,
                pixelSize: 5 //像素点大小
            }
        });
        that._viewer.entities.add(pt);
    });
    return true;
};
GPSHandler.prototype.RemoveOutputGps = function() {
    // let that = this;
    // let viewer = that._viewer;
    // let entitys = viewer.entities._entities._array;
    // for (let i = 0; i < entitys.length; i++) {
    //     if (entitys[i]._name === "trajectory") {
    //         viewer.entities.remove(entitys[i]);
    //         i--;
    //     }
    // }
};

GPSHandler.prototype.showCurrentPosition = function() {
    if (!this.getServiceStatus()) return false;
    let str = JavaContext.getCurrentGPS();
    if (str.length < 1) return true;
    let that = this;
    let pos = JSON.parse(str);
    let pt = new Cesium.Entity({
        name: "trajectory",
        position: Cesium.Cartesian3.fromDegrees(pos.lon, pos.lat),
        point: {
            color: Cesium.Color.AZURE, //点位颜色
            outlineColor: Cesium.Color.WHITE,
            outlineWidth: 2,
            pixelSize: 10 //像素点大小
        }
    });
    if (that._currentPointEntity != null) {
        that._viewer.entities.remove(that._currentPointEntity);
    }
    that._viewer.entities.add(pt);
    that._currentPointEntity = pt;
    that._viewer.camera.flyTo({
        //相机飞往该点
        destination: Cesium.Cartesian3.fromDegrees(pos.lon, pos.lat, 10000), //摄像机的最终位置
        duration: 3,
        orientation: new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(0), Cesium.Math.toRadians(-89), Cesium.Math.toRadians(0))
    });
    return true;
};

export default GPSHandler;
